/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txcms.dao;

import cn.txdnet.txcms.domain.Infotags;
import cn.txdnet.txcms.dto.TagsDTO;
import cn.txdnet.txcms.mapper.InfotagsMapper;
import cn.txdnet.txweb.utils.CollectionUtil;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author txdnet
 */
public class InfotagsDAO extends MybatisBaseDAO {

    /**
     * 批量插入info关联tagid
     *
     * @param list
     * @return
     */
    public int insert(List<Infotags> list) {
        InfotagsMapper mapper = this.getMapper(InfotagsMapper.class);
        this.changeSessionExecutorTypeToBatch();
        int effectNum = 0;
        for (Infotags infotags : list) {
            if (infotags.validate()) {
                effectNum += mapper.insert(infotags);
            }
        }
        return effectNum;
    }

    /**
     * 批量删除info 关联 tagid
     *
     * @param infoIdArr
     * @return
     */
    public int delete(long[] infoIdArr) {
        InfotagsMapper mapper = this.getMapper(InfotagsMapper.class);
        this.changeSessionExecutorTypeToBatch();
        int effectNum = 0;
        for (long infoid : infoIdArr) {
            effectNum += mapper.delete(infoid);
        }
        return effectNum;
    }

    /**
     * 查询info关联tagid
     *
     * @param infoid
     * @return
     */
    public List<?> queryList(Long infoid) {
        InfotagsMapper mapper = this.getMapper(InfotagsMapper.class);
        return mapper.queryList(infoid);
    }

    /**
     * 查询info关联tagid
     *
     * @param infoid
     * @return
     */
    public List<?> queryTagidList(Long infoid) {
        InfotagsMapper mapper = this.getMapper(InfotagsMapper.class);
        return mapper.queryTagidList(infoid);
    }

    /**
     * 前台查询info 关联的tags
     * @param infoid
     * @return 
     */
    public List<TagsDTO> queryInfoTagsForView(List<Long> idList) {
        InfotagsMapper mapper = this.getMapper(InfotagsMapper.class);
        return mapper.queryInfoTagsForView(idList);
    }

    /**
     * 更新info关联tagid
     *
     * @param infoid
     * @param list
     * @return
     */
    public int update(Long infoid, List<Infotags> list, List<?> oldTagidList) {
        InfotagsMapper mapper = this.getMapper(InfotagsMapper.class);
        // 先删除旧tag
        if (CollectionUtil.isNotEmpty(oldTagidList)) {
            mapper.delete(infoid);
        }
        // 再插入新tag
        this.changeSessionExecutorTypeToBatch();
        int effectNum = 0;
        for (Infotags infotags : list) {
            if (infotags.validate()) {
                effectNum += mapper.insert(infotags);
            }
        }
        return effectNum;
    }
}
